package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.exception.CustException;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadLocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
@Slf4j
@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryServiceImpl apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        // 1 校验参数 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "当前用户未登录!");
        }
        // 2 根据当前登录用户id查询行为实体对象
        Integer id = user.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(id, null);
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体对象不存在或者创建失败!");
        }
        Short operation = dto.getOperation();
        // 3 如果是点赞操作
        if ((short) 0 == operation) {
            //  判断是否已经点过赞
            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("operation").is(operation).and("entryId").is(apBehaviorEntry.getId()));
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (apLikesBehavior != null) {
                // 说明已经点过赞了
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "已经点过赞了!");
            }
            // 4 没有点过赞则 向mongo点赞集合中 添加点赞数据
            ApLikesBehavior apLikesBehavior1 = new ApLikesBehavior();
            apLikesBehavior1.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior1.setArticleId(dto.getArticleId());
            apLikesBehavior1.setOperation((short) 0);
            apLikesBehavior1.setType((short) 0);
            apLikesBehavior1.setCreatedTime(new Date());
            ApLikesBehavior save = mongoTemplate.save(apLikesBehavior1);
            System.out.println(save);
        } else {
            // 5 如果是取消点赞操作  在mongo点赞集合中 删除对应点赞数据
            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
            DeleteResult remove = mongoTemplate.remove(query, ApLikesBehavior.class);
            System.out.println(remove);
        }
        // 发送新行为消息
        NewBehaviorDTO newBehavior = new NewBehaviorDTO();
        int add = dto.getOperation().intValue() == 0 ? 1 : -1;
        newBehavior.setAdd(add);
        newBehavior.setType(NewBehaviorDTO.BehaviorType.LIKES);
        newBehavior.setArticleId(dto.getArticleId());
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(newBehavior));
        log.info("成功发送 文章点赞行为消息,消息内容: {}" ,newBehavior);
        return ResponseResult.okResult();
    }
}
